package com.education.realtime.app.dws;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.education.realtime.app.BaseAppV1;
import com.education.realtime.bean.Play;
import com.education.realtime.bean.TestExamCourse;
import com.education.realtime.common.Constant;
import com.education.realtime.fuction.DimAsyncFunction;
import com.education.realtime.util.AtguiguUtil;
import com.education.realtime.util.FlinkSinkUtil;
import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.api.common.functions.ReduceFunction;
import org.apache.flink.streaming.api.datastream.AsyncDataStream;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.windowing.ProcessAllWindowFunction;
import org.apache.flink.streaming.api.windowing.assigners.TumblingEventTimeWindows;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.streaming.api.windowing.windows.TimeWindow;
import org.apache.flink.util.Collector;

import java.time.Duration;
import java.util.concurrent.TimeUnit;

public class Dws_Play extends BaseAppV1 {
    public static void main(String[] args) {
        new Dws_Play().init(3004,"Dws_Play", Constant.TOPIC_DWD_TRAFFIC_APPVIDEO);
    }
    @Override
    protected void handle(StreamExecutionEnvironment env, DataStreamSource<String> stream) {
        SingleOutputStreamOperator<Play> examCourse = parseToBean(stream);
        SingleOutputStreamOperator<Play> jo = joinDim(examCourse);
        jo.assignTimestampsAndWatermarks(WatermarkStrategy.<Play>forBoundedOutOfOrderness(Duration.ofSeconds(5))
                .withTimestampAssigner((obj,ts)->obj.getTs()*1000))
                .keyBy(Play::getChapter_id)
                .windowAll(TumblingEventTimeWindows.of(Time.seconds(5)))
                .reduce(new ReduceFunction<Play>() {
                    @Override
                    public Play reduce(Play t2, Play t1) throws Exception {
                        t1.setUser_sum(t1.getUser_sum() + t2.getUser_sum());
                        t1.setVideo_view(t1.getVideo_view() + t2.getVideo_view());
                        return t1;
                    }
                }, new ProcessAllWindowFunction<Play, Play, TimeWindow>() {
                    @Override
                    public void process(Context context, Iterable<Play> iterable,
                                        Collector<Play> collector) throws Exception {
                        Play bean = iterable.iterator().next();
                        String stt = AtguiguUtil.toDateTime(context.window().getStart());
                        String edt = AtguiguUtil.toDateTime(context.window().getEnd());
                        bean.setStt(stt);
                        bean.setEdt(edt);
                        collector.collect(bean);

                    }
                })
                .addSink(FlinkSinkUtil.getClickHouseSink(Constant.TOPIC_ODS_DB,"dws_play_window",Play.class));


    }

    private SingleOutputStreamOperator<Play> joinDim(SingleOutputStreamOperator<Play> examCourse) {
        return AsyncDataStream.unorderedWait(examCourse,
                new DimAsyncFunction<Play>(){

                    @Override
                    public void addDim(Play input, JSONObject dim) {
                        input.setChapter_id(dim.getString("course_id"));
                    }

                    @Override
                    public String getTableName() {
                        return "dim_test_paper";
                    }

                    @Override
                    public String getDimId(Play input) {
                        return input.getChapter_id();
                    }
                },
                120,
                TimeUnit.SECONDS);

    }

    private SingleOutputStreamOperator<Play> parseToBean(DataStreamSource<String> stream) {
    return     stream.map(JSON::parseObject).map(obj -> Play.builder().id(obj.getString("id")).stt("").edt("").user_sum(1L).video_view(obj.getLong("position_sec")).ts(obj.getLong("ts")).build());
    }
}
